﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DropDownListDome
{
    public partial class Dome_1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDdl();
            }

            CheckDdlIsExistValue(ddlNames, "2");
            CheckDdlIsExistValue(ddlNames, "1");
            CheckDdlIsExistValue(ddlNames, "3");
        }

        private void BindDdl()
        {
            string sqlText = "Select * from BaseUser";
            DataTable dt = SqlUtil.GetTable(SqlUtil.ConfigString, CommandType.Text, sqlText);
            ddlNames.DataSource = dt;
            ddlNames.DataTextField = "Name";
            ddlNames.DataValueField = "Id";
            ddlNames.DataBind();

            ddlNames.Items.Insert(0, new ListItem("", "0"));
            ddlNames.SelectedIndex = 0;
        }

        public bool CheckDdlIsExistValue(DropDownList ddl, string value)
        {
            //bool res = false;
            //foreach (ListItem item in ddl.Items)
            //{
            //    if (item.Value == value)
            //    {
            //        res = true;
            //        break;
            //    }
            //}
            //return res;
            ListItem item = ddl.Items.FindByValue(value);
            return true;
        }
    }
}